home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / The Timer and Time / DigitalClockWithDate / DigitalClockWithDate.cs next >
Encoding:
Text File  |  2001-01-15  |  1.3 KB  |  36 lines

  1. //---------------------------------------------------
  2. // DigitalClockWithDate.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class DigitalClockWithDate: DigitalClock
  9. {
  10.      public new static void Main()
  11.      {
  12.           Application.Run(new DigitalClockWithDate());
  13.      }
  14.      public DigitalClockWithDate()
  15.      {
  16.           Text += " with Date";
  17.      }
  18.      protected override void OnPaint(PaintEventArgs pea)
  19.      {
  20.           Graphics grfx    = pea.Graphics;
  21.           DateTime dt      = DateTime.Now;
  22.           string   strTime = dt.ToString("d") + "\n" + dt.ToString("T");
  23.           SizeF    sizef   = grfx.MeasureString(strTime, Font);
  24.           float    fScale  = Math.Min(ClientSize.Width  / sizef.Width,
  25.                                       ClientSize.Height / sizef.Height);
  26.           Font     font    = new Font(Font.FontFamily,
  27.                                       fScale * Font.SizeInPoints);
  28.  
  29.           StringFormat strfmt = new StringFormat();
  30.           strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
  31.  
  32.           grfx.DrawString(strTime, font, new SolidBrush(ForeColor), 
  33.                           ClientRectangle, strfmt);
  34.      }
  35. }
  36.